package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.model.Discover;
import org.rcloud.io.service.DiscoverService;
import org.rcloud.io.service.WxPushServiceQcl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * Created by Administrator on 2019/3/31.
 */
@RestController
@RequestMapping(value = "/Discover")
public class DiscoverController {
    @Autowired
    private DiscoverService discoverService;

    @RequestMapping(value = "/add",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public int add(@RequestBody String params){
        Discover discover = JSONObject.parseObject(params,Discover.class);
        Discover discover1 = (Discover) discoverService.selectByPrimaryKey(discover).getInfo();

        //用 before 和 after 进行时间比较================方式一
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String date1 = "2019-04-03 00:00:00";
//        String date2 = sdf.format(new Date());
//        Date date_01 = null;
//        Date date_02 = null;
//        try {
//            date_01 = sdf.parse(date1);
//            date_02 = sdf.parse(date2);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        if ((date_02.before(date_01)) == false) {}

        // 转换成  毫秒数  进行时间比较===============方式二
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date2 = "2019-04-25 17:00:00";
        Long time = null;
        try {
            time = sdf.parse(date2).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //报名时间截止与4月3号
        if (time < System.currentTimeMillis()){
            System.out.println("=====寻美评美报名时间已截止！======");
            return 3;
        }else {
            if (null!=discover1){
                System.out.println("=====寻美评美已报名！======");
                return 2;
            }else {
                JSONObject object = JSONObject.parseObject(params);
                object.put("id", UUID.randomUUID());
                object.put("createtime",new Date());
                object.put("flag",1);
                Discover discover2 = JSON.parseObject(object.toString(),Discover.class);
                if (Integer.valueOf(discoverService.insertSelective(discover2).getInfo().toString()) == 1){
                    System.out.println("=======报名成功，推送模板消息！=======");
                    WxPushServiceQcl wxPushServiceQcl = new WxPushServiceQcl();
                    wxPushServiceQcl.pushOneUser4(discover2.getUserid(),discover2.getFramid(),discover2.getActivityname(),discover2.getName(),
                            discover2.getSex(),discover2.getAge(),discover2.getTelephone(),discover2.getCreatetime());
                    return 1;
                }else {
                    System.out.println("=======报名失败！=======");
                    return 0;
                }
            }
        }
    }

    /**
     * 查询报名统计
     * */
    @RequestMapping(value = "/selectAll", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @CrossOrigin
    public PageVo<Discover> selectAll(){
        return discoverService.selectAll();
    }
}
